/**
 * @Author: ufec
 * @Date: 2021-09-01 18:27:24
 * @LastEditTime: 2021-09-01 18:28:45
 * @LastEditors: ufec
 * @Description: https://github.com/ufec
 */

/**
 * 获取奖品
 * @param {Array} prizes 奖品列表
 * @param {String} prob_key 概率key
 * @returns 
 */
function getLotteryRes(prizes, prob_key = 'prob') {
  let sum = 0;
  let result = null;
  for (const item of prizes) {
    sum += item[prob_key];
  }
  for (const item of prizes) {
    const rand = Math.floor(Math.random() * sum);
    if (rand <= item[prob_key]) {
      result = item;
      break;
    } else {
      sum -= item[prob_key];
    }
  }
  return result;
}

module.exports = {
  getLotteryRes
};